package es.unileon.is.finalproyect.userinterface;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import es.unileon.is.finalproyect.texts.Question;
import es.unileon.is.finalproyect.texts.Text;
import es.unileon.is.finalproyect.users.User;


public class TextReviewInterface extends JFrame {
	


	public TextReviewInterface(Text t,User u) {
		
		setSize(400,400);
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		//*****modificaciones para JAVAHELP*******
		JMenuBar menuBar = new JMenuBar();
		JMenu menuHelp = new JMenu("Help");
		JMenuItem help = new JMenuItem("Show help");
		menuBar.add(Box.createHorizontalGlue());
		menuBar.add(menuHelp);
		menuHelp.add(help);
		setJMenuBar(menuBar);
		//metodo para dar la accion a help
		ViewHelp.showHelp(help, getContentPane(), "ventana_revision-texto");
		//*****************************************
		
		//Ponemos un scroll a la izq, le a�adimos un textarea al scroll y metemos el texto
		JTextArea textAreaL = new JTextArea();
		textAreaL.setText(new String(t.getData()));
		JScrollPane scrollT = new JScrollPane(textAreaL);
		getContentPane().add(scrollT, BorderLayout.CENTER);
		
		//Un boton en el sur
		
		JPanel buttonPane = new JPanel();
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		
		buttonPane.add(Box.createHorizontalGlue());
		Date dateOfExam = u.getDateOfExam(t);
		Calendar c = Calendar.getInstance();
		c.setTime(dateOfExam);
		String date = "Date of Exam: Month " + c.get(Calendar.MONTH) + ", " +c.get(Calendar.DAY_OF_MONTH) +" "+ Character.toString('\u036D') + Character.toString('\u02B0') + " " + c.get(Calendar.YEAR);  
		JLabel dateLabel = new JLabel(date);
		buttonPane.add(dateLabel);
		JLabel markLabel = new JLabel("Mark: " + u.getMarkToText(t));
		buttonPane.add(Box.createRigidArea(new Dimension(20,0)));
		buttonPane.add(markLabel);
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				dispose();
			}
		});
		buttonPane.add(Box.createRigidArea(new Dimension(20,0)));
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
		
		
		
		//Ponemos un scroll a la dere, le a�adimos un textArea al scroll y metemos las preguntas con su respuesta y la correcta.
		
		
		JPanel panelRight = new JPanel(null);
		JScrollPane scrollQ = new JScrollPane(panelRight);
		getContentPane().add(scrollQ, BorderLayout.EAST);

		Map<Question,Integer> contestacion = u.getAnswersToText(t);
		int x = 10;
		int y = 10;
		ImageIcon okIcon = new ImageIcon("img/ok.png");
		ImageIcon wrongIcon = new ImageIcon("img/wrong.png");
		int iconWidth = okIcon.getIconWidth();
		int iconHeight = okIcon.getIconHeight();
		for(Map.Entry<Question,Integer> entry:contestacion.entrySet()){
			String randomQuestion = entry.getKey().getQuestion();
			JLabel title = new JLabel(randomQuestion);
			int answer = entry.getValue();
			String[] answers = entry.getKey().getAnswers();
			
			int titleWidth = (int) title.getPreferredSize().getWidth();
			int titleHeight = (int) title.getPreferredSize().getHeight();
			title.setBounds(x,y,titleWidth, titleHeight);
			panelRight.add(title);
			ImageLabel icono = null;
			if(answer == 1){
				icono = new ImageLabel(okIcon);
			} else {
				icono = new ImageLabel(wrongIcon);
			}
			icono.setBounds(x+titleWidth+10,y, iconWidth, iconHeight);
			panelRight.add(icono);
			y += Math.max(iconHeight, titleHeight);
			for (int i=0;i<4;i++){
				JLabel answerLabel = new JLabel(answers[i]);
				if(i==0){
					ImageLabel okLabel = new ImageLabel(okIcon);
					okLabel.setBounds(x,y,iconWidth,iconHeight);
					panelRight.add(okLabel);	
				} else if(i==(answer-1)){
					ImageLabel wrongLabel = new ImageLabel(wrongIcon);
					wrongLabel.setBounds(x,y,iconWidth,iconHeight);
					panelRight.add(wrongLabel);
				}
				int labelWidth = (int) answerLabel.getPreferredSize().getWidth();
				int labelHeight = (int) answerLabel.getPreferredSize().getHeight();
				answerLabel.setBounds(x + iconWidth+10,y, labelWidth, labelHeight);
				panelRight.add(answerLabel);
				y+=Math.max(iconWidth, labelHeight) + 10;
			}
			
			panelRight.setPreferredSize(new Dimension(400,y));
		}
		
		
	}


}
